<?php

namespace App\Http\Controllers\Api\Meeting;

use App\Http\Controllers\Controller;
use App\Models\MeetingActivity;
use App\Models\MeetingActivityEnroll;
use Illuminate\Http\Request;

class ActivityController extends Controller
{
    public function get(Request $request)
    {
        $activity = MeetingActivity::find($request->input('id'));
        return ajaxReturn(['activity' => $activity]);
    }

    public function batchget(Request $request)
    {
        $query = MeetingActivity::query();
        return ajaxReturn([
            'total' => $query->count(),
            'items' => $query->offset($request->input('offset', 0))
                ->limit($request->input('count', 10))->get()
        ]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function enroll(Request $request)
    {
        $attributes = $request->input('enroll', []);
        if (!$enroll = MeetingActivityEnroll::where($attributes)->first()) {
            $enroll = new MeetingActivityEnroll();
            $enroll->fill($attributes)->save();
        }

        return ajaxReturn(['enroll' => $enroll]);
    }
}
